package com.example.demo.widget.chart.histogram

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import com.example.demo.widget.chart.shadow.ShadowConfig
import com.example.demo.widget.chart.utils.DrawTextUtils
import kotlin.math.roundToInt

class DrawInfoHelper(val builder: DrawInfoBuilder) {

    private val mBackground: Drawable
    private val mTitlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val mPartPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    init {
        mBackground = ShadowConfig.Builder()
            .setShadowRadius(builder.shadowSize)
            .setRadius(builder.rectRadius)
            .setColor(builder.contentBgColor)
            .builder()

        mTitlePaint.color = builder.titleTextColor
        mTitlePaint.isFakeBoldText = builder.titleTextBold
        mTitlePaint.textSize = builder.titleTextSize

        mPartPaint.textSize = builder.itemTextSize
        mPartPaint.isFakeBoldText = builder.itemTextBold
    }

    fun drawInfo(
        canvas: Canvas,
        item: HistogramItem,
        xInView: Float,
        yInView: Float,
        viewDrawRect: Rect) {

        val list = item.partList
        val size = list.size

        var maxLength = mTitlePaint.measureText(item.name)
        list.forEach {
            it.partText = "${it.name}: ${it.value}"
            val length = mPartPaint.measureText(it.partText)
            if(length > maxLength){
                maxLength = length
            }

        }
        val width = 2 * builder.shadowSize + maxLength + builder.paddingLeft + builder.paddingRight

        val itemTotalHeight = size * (builder.itemTextPadding + builder.itemTextSize) - builder.itemTextPadding

        val titleHeight =  builder.titleTextSize + builder.titleToBottom
        val yPadding = builder.paddingTop + builder.paddingBottom
        val height = 2 * builder.shadowSize + itemTotalHeight + titleHeight + yPadding


        var left = xInView
        var right = left + width
        var top = yInView
        var bottom = top + height

        if(right > viewDrawRect.right){
            val delta = right - viewDrawRect.right
            right -= delta
            left -= delta
        }

        if(bottom > viewDrawRect.bottom){
            val delta = bottom - viewDrawRect.bottom
            top -= delta
            bottom -= delta
        }
        mDrawRect.left = left
        mDrawRect.right = right
        mDrawRect.top = top
        mDrawRect.bottom = bottom

        mBackground.setBounds(left.roundToInt(),top.roundToInt(),right.roundToInt(),bottom.roundToInt())
        mBackground.draw(canvas)

        val drawLeft = left + builder.shadowSize + builder.paddingLeft
        var drawTop = top + builder.shadowSize + builder.paddingTop
        val drawRight = drawLeft + maxLength

        DrawTextUtils.drawTextOnRectCenterLeft(canvas,mTitlePaint,item.name,drawLeft,drawTop,drawRight,drawTop + builder.titleTextSize)
        drawTop += builder.titleTextSize + builder.titleToBottom
        val partHeight = builder.itemTextPadding + builder.itemTextSize
        list.forEach {
            mPartPaint.color = it.color
            DrawTextUtils.drawTextOnRectCenterLeft(canvas,mPartPaint,it.partText,
                drawLeft,drawTop,drawRight,drawTop + builder.itemTextSize)
            drawTop += partHeight
        }
    }

    private val mDrawRect = RectF()

    fun isPointInInfoView(x: Float,y: Float): Boolean {
        return mDrawRect.contains(x,y)
    }
}